<?php

/**
 * CodeLeaveType form base class.
 *
 * @method CodeLeaveType getObject() Returns the current form's model object
 *
 * @package    Amawal Invest
 * @subpackage form
 * @author     Ameen Shoman<amin.shoman@gmail.com>
 */
abstract class BaseCodeLeaveTypeForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                       => new sfWidgetFormInputHidden(),
      'out_fn'                   => new sfWidgetFormInputText(),
      'in_fn'                    => new sfWidgetFormInputText(),
      'out_of_calc'              => new sfWidgetFormInputCheckbox(),
      'max_period'               => new sfWidgetFormDateTime(),
      'branch_id'                => new sfWidgetFormPropelChoice(array('model' => 'Branch', 'add_empty' => true)),
      'link_employee_leave_list' => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'Employee')),
    ));

    $this->setValidators(array(
      'id'                       => new sfValidatorPropelChoice(array('model' => 'CodeLeaveType', 'column' => 'id', 'required' => false)),
      'out_fn'                   => new sfValidatorString(array('max_length' => 5, 'required' => false)),
      'in_fn'                    => new sfValidatorString(array('max_length' => 5, 'required' => false)),
      'out_of_calc'              => new sfValidatorBoolean(array('required' => false)),
      'max_period'               => new sfValidatorDateTime(),
      'branch_id'                => new sfValidatorPropelChoice(array('model' => 'Branch', 'column' => 'id', 'required' => false)),
      'link_employee_leave_list' => new sfValidatorPropelChoice(array('multiple' => true, 'model' => 'Employee', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('code_leave_type[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'CodeLeaveType';
  }

  public function getI18nModelName()
  {
    return 'CodeLeaveTypeI18n';
  }

  public function getI18nFormClass()
  {
    return 'CodeLeaveTypeI18nForm';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['link_employee_leave_list']))
    {
      $values = array();
      foreach ($this->object->getLinkEmployeeLeaves() as $obj)
      {
        $values[] = $obj->getEmployeeId();
      }

      $this->setDefault('link_employee_leave_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveLinkEmployeeLeaveList($con);
  }

  public function saveLinkEmployeeLeaveList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['link_employee_leave_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(LinkEmployeeLeavePeer::LEAVE_TYPE_ID, $this->object->getPrimaryKey());
    LinkEmployeeLeavePeer::doDelete($c, $con);

    $values = $this->getValue('link_employee_leave_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new LinkEmployeeLeave();
        $obj->setLeaveTypeId($this->object->getPrimaryKey());
        $obj->setEmployeeId($value);
        $obj->save();
      }
    }
  }

}
